package com.neutech.tank;

import com.neutech.Util.ImageUtils;
import com.neutech.base.*;
import com.neutech.constant.Constant;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;

import java.awt.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;


public class Bullet extends Sprite implements Drawable, Moveable {
  private Map<String,Image> imgMap = new HashMap<>();
    private transient List<Image> imageList = new ArrayList<>();
    public List<Boom> boomList = new CopyOnWriteArrayList<>();
  public Direction direction;
  private int speed = 5;
  public Boom boom;
  public void music(String path){
      new Thread(new Runnable() {
          @Override
          public void run() {
              FileInputStream is = null;
              AudioStream os = null;
              try {
                  is = new FileInputStream(path);
                  os = new AudioStream(is);
                  AudioPlayer.player.start(os);
              } catch (FileNotFoundException e) {
                  e.printStackTrace();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }).start();
  }
    /**
     * 给以接受地方坦克，再给他哥构造函数，就相当于两个不同的子弹,她俩只有一个不位null
     */
  private EnemyTank enemyTank;
  private Tank tank;

    @Override
    public void draw(Graphics g) {
        if (direction == Direction.UP && getY() > Constant.MARGIN_TOP){
            g.drawImage(imgMap.get("bullet_up"),getX(),getY(),null);
        }
        if (direction == Direction.RIGHT && getX() < Constant.BLACK_SIZE){
            g.drawImage(imgMap.get("bullet_right"),getX(),getY(),null);

        }
        if (direction == Direction.DOWN && getY() < Constant.BLACK_SIZE){
            g.drawImage(imgMap.get("bullet_down"),getX(),getY(),null);

        }
        if (direction == Direction.LEFT && getX() > Constant.MARGIN_LEFT){
            g.drawImage(imgMap.get("bullet_left"),getX(),getY(),null);

        }
     move();

    }

    @Override
    public void move() {
     if (direction == Direction.UP){
       setY(getY()-speed);
     }
      if (direction == Direction.RIGHT){
        setX(getX()+speed);
      }
      if (direction == Direction.DOWN){
        setY(getY()+speed);
      }
      if (direction == Direction.LEFT){
      setX(getX()-speed);
      }
      outOfBounds();

    }
    public void outOfBounds(){
        if (tank !=null){
            if (direction == Direction.UP){
                /**
                 *  x代表左右，左面是0，右面是正无穷大，y上面是0，下面是正无穷大
                 */
                if (getY() <= Constant.MARGIN_TOP){
                    tank.bulletList.remove(this);
                    music("src/com/neutech/music/buh.wav");
                }
            }
            if (direction == Direction.RIGHT){
                if (getX() >= Constant.BLACK_SIZE){
                    tank.bulletList.remove(this);
                    music("src/com/neutech/music/buh.wav");
                }

            }
            if (direction == Direction.DOWN){
                if (getY() >=Constant.BLACK_SIZE){
                    tank.bulletList.remove(this);
                    music("src/com/neutech/music/buh.wav");
                }
            }
            if (direction == Direction.LEFT){
                if (getX()<= Constant.MARGIN_LEFT){
                    tank.bulletList.remove(this);
                    music("src/com/neutech/music/buh.wav");

                }
            }
        }
        if (enemyTank != null){
            if (direction == Direction.UP){
                /**
                 *  x代表左右，左面是0，右面是正无穷大，y上面是0，下面是正无穷大
                 */
                if (getY() <= Constant.MARGIN_TOP){
                    enemyTank.bulletList.remove(this);

                }
            }
            if (direction == Direction.RIGHT){
                if (getX() >= Constant.BLACK_SIZE){
                    enemyTank.bulletList.remove(this);
                }

            }
            if (direction == Direction.DOWN){
                if (getY() >=Constant.BLACK_SIZE){
                    enemyTank.bulletList.remove(this);
                }
            }
            if (direction == Direction.LEFT){
                if (getX()<= Constant.MARGIN_LEFT){
                    enemyTank.bulletList.remove(this);

                }
            }

        }


    }

    /**
     * 下面的俩个必须一样的样式
     * @param x
     * @param y
     * @param width
     * @param height
     * @param direction
     * @param enemyTank
     */
    /**
     * 构造函数同名，解决办法就是用静态方法代替构造函数
     *
     */

//    public static Bullet getTankBullet(int x, int y, int width, int height, Direction direction, Tank tank){
//        Bullet bullet = new Bullet(x,y,width,height,direction,tank);
//        return bullet;
//    }
//    public static Bullet getEnembullent(int x, int y, int width, int height,Direction direction,EnemyTank enemyTank){
//        /* new自己给参数 */
//       Bullet bullet = new Bullet(x,y,width,height,direction,enemyTank);
//       return bullet;
//    }
//
    public Bullet(int x, int y, int width, int height,int speed,Direction direction,EnemyTank enemyTank) {
        super(x, y, width, height);
        this.speed = speed;
        this.enemyTank = enemyTank;
        this.direction = direction;
        loadingImg();
    }

    public Bullet(int x, int y, int width, int height, int speed,Direction direction, Tank tank) {
        super(x, y, width, height);
        this.speed = speed;
        this.tank = tank;
      /**
       * 这里的dir等于哪里穿过来的dir
       */
      this.direction = direction;
        loadingImg();
    }

    public Bullet() {

    }

    /**
     * public class Bullet extends Sprite implements Drawable, Moveable 因为上面这个继承精灵类，所以得在有个重写方法，
     * @return
     */
    @Override
    public Rectangle getRectangle() {
        return new  Rectangle(getX(),getY(),getWidth(),getHeight());
    }

    public void loadingImg(){
//    imgMap.put("right", ImageUtils.getValue("bullet"));
    imgMap.put("bullet_up", ImageUtils.getValue("bullet_up"));
    imgMap.put("bullet_right", ImageUtils.getValue("bullet_right"));
    imgMap.put("bullet_down", ImageUtils.getValue("bullet_down"));
    imgMap.put("bullet_left", ImageUtils.getValue("bullet_left"));

    }
}
